package com.biz.primus.ms.adv.trans;

import com.biz.primus.ms.adv.dao.po.AdPosition;
import com.biz.primus.ms.adv.dao.po.Advertisement;
import com.biz.primus.ms.adv.dao.ro.AdPositionRo;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;

import java.util.Set;

/**
 * @author jun.liu
 * @date 2017/9/18
 */
public class AdPosition2AdPositionRo implements Function<AdPosition, AdPositionRo> {

    @Override
    public AdPositionRo apply(AdPosition input) {
        AdPositionRo ro = new AdPositionRo();
        if (input.getId() != null) {
            ro.setId(input.getId());
        }
        ro.setName(input.getName());
        ro.setWidth(input.getWidth());
        ro.setHeight(input.getHeight());
        ro.setDescription(input.getDescription());
        ro.setUseType(input.getUseType());
        ro.setInvalid(input.isInvalid());
        if (CollectionUtils.isNotEmpty(input.getAds())) {
            Set<Long> idSet = Sets.newHashSet(Lists.transform(input.getAds(), new Function<Advertisement, Long>() {
                @Override
                public Long apply(Advertisement input) {
                    return input.getId();
                }
            }));
            ro.setAdvertisementId(Joiner.on(",").join(idSet));
        }
        ro.setAdUsePlace(input.getAdUsePlace());
        return ro;
    }
}
